package com.jxw.cloudpen.web.palpay;

import com.google.gson.Gson;
import com.paypal.api.payments.*;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.PayPalRESTException;

import com.paypal.payments.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ligang
 * @create 2025/5/19 15:50
 */
@Service
public class PayPalServiceImpl implements PayPalService {

    @Autowired
    private APIContext apiContext;


//    Payment payment = paypalService.createPayment(
//            500.00,
//            "USD",
//            PaypalPaymentMethod.paypal,
//            PaypalPaymentIntent.sale,
//            "payment description",
//            cancelUrl,
//            successUrl);
    @Override
    public Payment createPayment(Double total, String currency, String method, String intent, String description, String cancelUrl, String successUrl) throws PayPalRESTException {

        Amount amount = new Amount();
        amount.setCurrency(currency);
        amount.setTotal(String.format("%.2f", total));

        Transaction transaction = new Transaction();
        transaction.setDescription(description);
        transaction.setAmount(amount);
        List<Transaction> transactions = new ArrayList<>();
        transactions.add(transaction);

        Payer payer = new Payer();
        payer.setPaymentMethod(method);

        Payment payment = new Payment();
        payment.setIntent(intent);
        payment.setPayer(payer);
        payment.setTransactions(transactions);

        RedirectUrls redirectUrls = new RedirectUrls();
        redirectUrls.setCancelUrl(cancelUrl);
        redirectUrls.setReturnUrl(successUrl);
        payment.setRedirectUrls(redirectUrls);

        return payment.create(apiContext);
    }

    @Override
    public Payment executePayment(String paymentId, String payerId) throws PayPalRESTException {
        Payment payment = new Payment();
        payment.setId(paymentId);
        PaymentExecution paymentExecute = new PaymentExecution();
        paymentExecute.setPayerId(payerId);
        return payment.execute(apiContext, paymentExecute);
    }

    @Override
    public Refund refundPayment(String captureId, String amount, String currency) throws PayPalRESTException {
        RefundRequest refundRequest = new RefundRequest();

        // 设置退款金额
        Amount money = new Amount();
        money.setCurrency(currency);
        money.setTotal(amount);
        refundRequest.setAmount(money);
        // 设置退款说明（可选）
        refundRequest.setDescription("商品退款");
        refundRequest.setReason("客户要求退款");
        // 执行退款
        Capture capture = new Capture();
        capture.setId(captureId);
        Refund refund = capture.refund(apiContext, refundRequest);
        Gson gson = new Gson();
        String json = gson.toJson(refund);

        System.out.println("resultPayment is {}"+ json);
        return refund;
    }

    @Override
    public Refund getRefundDetails(String refundId) throws PayPalRESTException {
        return Refund.get(apiContext, refundId);
    }


}
